package com.meis.base.mei.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.meis.base.mei.entity.Result;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class ParseJsonUtils {

    public static String toJson(Object object) {
        try {
            return new Gson().toJson(object);
        } catch (Exception e) {
        }
        return "";
    }

    public static <T> T parseData(String json, Class<T> t) {
        return new Gson().fromJson(json, t);
    }

    public static <T> Result<T> parseDataToResult(String json, Class<T> clazz) {
        Type type = new ParameterizedTypeImpl(Result.class, new Class[]{clazz});
        return new Gson().fromJson(json, type);
    }

    public static <T> List<T> parseListData(String json, Class<T> clazz) {
        Type listType = new ParameterizedTypeImpl(List.class, new Class[]{clazz});
        try {
            return new Gson().fromJson(json, listType);
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    public static <T> List<T> parseListData(String json, String field, Class<T> clazz) {
        if (TextUtils.isEmpty(json)) {
            return new ArrayList<T>();
        }
        try {
            JSONObject jsonObject = new JSONObject(json);
            if (jsonObject.has(field)) {
                return parseListData(jsonObject.optString(field), clazz);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return new ArrayList<T>();
    }

    public static <T> List<T> parseListByThreeFiled(String json, String firstField, String secondField, String threeField, Class<T> clazz) {
        if (TextUtils.isEmpty(json)) {
            return new ArrayList<T>();
        }
        try {
            JSONObject firObj = new JSONObject(json);
            if (firObj.has(firstField)) {
                JSONObject secObj = new JSONObject(firObj.optString(firstField));
                if (secObj.has(secondField)) {
                    JSONObject thrObj = new JSONObject(secObj.optString(secondField));
                    if (thrObj.has(threeField)) {
                        return parseListData(thrObj.optString(threeField), clazz);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return new ArrayList<T>();
    }

    public static <T> ArrayList<T> parseArrayListData(String json, Class<T> clazz) {
        Type listType = new ParameterizedTypeImpl(List.class, new Class[]{clazz});
        return new Gson().fromJson(json, listType);
    }

    public static String parseFieldData(String json, String fieldName) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            if (jsonObject.has(fieldName)) {
                return jsonObject.optString(fieldName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String parseResponseValue(String json, String valueName) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            if (jsonObject.has("code") && jsonObject.optString("code").equals("200") && jsonObject.has("data")) {
                JSONObject valueObj = new JSONObject(jsonObject.optString("data"));
                if (valueObj.has(valueName)) {
                    return valueObj.optString(valueName);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

}
